package cc.wsyw126.java.Thread;

import java.util.concurrent.CountDownLatch;

/**
 * Created by junpeng.yang on 16/11/5.
 */
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        Long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new runThread(1, countDownLatch));
            thread.start();
        }
        Long time = System.currentTimeMillis() - start;
        System.out.println("time = " + time);
        countDownLatch.await();
        time = System.currentTimeMillis() - start;
        System.out.println("time = " + time);
    }


}

class runThread implements Runnable {
    int i;
    CountDownLatch latch;

    public runThread(int i, CountDownLatch count) {
        i = i;
        latch = count;
    }

    @Override
    public void run() {
        System.out.println("i = " + i);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        latch.countDown();

    }
}